package com.caiyi.tips.core.util;

import lombok.extern.slf4j.Slf4j;
import org.jdom.Element;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/*
 * @author Cliff.ZhouYang
 * @date 2017/11/15 15:06
 */
@Slf4j
public class TipsUtil {
    public static String getRealIp(HttpServletRequest request) {
        List<String> proxyIpAddrs = new ArrayList<String>();
        List<String> ipHeaders = new ArrayList<String>();
        List<String> localareaIps = new ArrayList<String>();
        File file = new File("/opt/export/config/proxy-ip-config.xml");
/*        if (file != null && file.exists()) {
            JXmlWapper proxyIpConfig = JXmlWapper.parse(file);
            proxyIpAddrs = readProxyIpAddrs(proxyIpConfig);
            ipHeaders = readRequestHeaders(proxyIpConfig);
            localareaIps = readLocalareaIps(proxyIpConfig);
        }*/
        return getRealIpNew(request, proxyIpAddrs, ipHeaders, localareaIps);
    }
/*    private static List<String> readProxyIpAddrs(JXmlWapper proxyIpConfig) {
        List<String> latestProxyIpAddrs = new ArrayList<String>();
        @SuppressWarnings("unchecked")
        List<Element> addrs = proxyIpConfig.getXmlRoot().getChildren("addr");
        for (Element addr : addrs) {
            latestProxyIpAddrs.add(addr.getTextTrim());
        }
        return latestProxyIpAddrs;
    }

    private static List<String> readRequestHeaders(JXmlWapper proxyIpConfig) {
        List<String> requestHeaders = new ArrayList<String>();
        @SuppressWarnings("unchecked")
        List<Element> addrs = proxyIpConfig.getXmlRoot().getChildren("header");
        for (Element addr : addrs) {
            requestHeaders.add(addr.getTextTrim());
        }
        return requestHeaders;
    }

    private static List<String> readLocalareaIps(JXmlWapper proxyIpConfig) {
        List<String> requestHeaders = new ArrayList<String>();
        @SuppressWarnings("unchecked")
        List<Element> addrs = proxyIpConfig.getXmlRoot().getChildren("local");
        for (Element addr : addrs) {
            requestHeaders.add(addr.getTextTrim());
        }
        return requestHeaders;
    }*/

    public static String getRealIpNew(HttpServletRequest request, List<String> proxyIpAddrs, List<String> ipHeaders, List<String> localareaIps) {
        String ipStr = null;
        String[] ipAddrs = null;
        String clientIp = "";
        //优先获取cdn的ip信息
        String cdn = request.getHeader("cdn");
        if(!StringUtil.isEmpty(cdn) && !"null".equals(cdn)){
            clientIp = cdn;
            log.info("获取用户登录IP header==cdn,clientIp=" + clientIp);
            return clientIp;
        }
        for (String ipHeader : ipHeaders) {
            ipStr = request.getHeader(ipHeader);
            if (isUnknownIp(ipStr)) {
                continue;
            }
//			logger.info("获取用户登录IP header=="+ipHeader + "=" + ipStr);
//			log_ip.info("********获取用户登录IP信息头   header=="+ipHeader + "=" + ipStr);
            ipAddrs = ipStr.split(",");
            for (String ipAddr : ipAddrs) {
                // 第一个ip就是代理ip则不再使用这个头信息
                if (proxyIpAddrs.contains(ipAddr)) {
                    clientIp = "";
                    break;
                }
                clientIp = ipAddr;
                // ip是局域网ip则读取同一头信息里的下一个ip
                for (String localareaIp : localareaIps) {
                    if (clientIp.startsWith(localareaIp)) {
                        clientIp = "";
                        break;
                    }
                }
                if (!isUnknownIp(clientIp)) {
                    break;
                }
            }
            if (!isUnknownIp(clientIp)) {
                break;
            }
        }
        if (isUnknownIp(clientIp)) {
            if (!proxyIpAddrs.contains(request.getRemoteAddr())) {
                clientIp = request.getRemoteAddr();
//				logger.info("获取用户登录IP  remoteAddr=" + clientIp);
//				log_ip.info("********isUnknownIp********* 获取remoteAddr=" + clientIp);
            }
        }
//		log_ip.info("*********IP结果***********  clientIp=" + clientIp);
        return clientIp;
    }

    private static boolean isUnknownIp(String ip) {
        return StringUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip);
    }
}
